
<?php

// Remove goto hdUOMO_1Ah;
// Replace with:

use network\http\Client;

include './meihe/common.php';
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET,POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header('Content-Type: text/html; charset=utf-8');

// Remove goto T7Eei13Msc;
// Replace with:
session_start();
$value = NULL;
$islogin2 = $islogin2 ?? 0;
if (!$islogin2) {
	exit('{"code":-1,"msg":"请你先登录后再使用！"}');
}

// Remove goto Dkr45f2RIX;
// Replace with:
$format = $_REQUEST['format'] ?? '';
if (isset($_GET['type'])) {
	$vvalue = $_GET['type'];
} else {
	if (isset($_POST['type'])) {
		$vvalue = $_POST['type'];
	} else {
		$vvalue = $conf['duan'];
	}
}

// Remove goto n9iUPU89Bx;
// Replace with:
if (isset($_REQUEST['api'])) {
	$api = $_REQUEST['api'];
	if ($api == 1) {
		$fhfsb = '直链方式';
		$fhb = 'api/?';
	} elseif ($api == 2) {
		$fhfsb = '跳转方式';
		$fhb = 'api/t.php?';
	} elseif ($api == 3) {
		$fhfsb = '缩短方式';
		$fhb = 'api/s.php?';
	} else {
		exit('{"msg":"防洪方式不存在！"}');
	}
} else {
	exit('{"msg":"防洪方式不存在！"}');
}

// Remove goto rtpJWOkRfL;
// Replace with:
$value = $_REQUEST['longurl'] ?? '';

if (empty($value)) {
	exit('{"msg":"URL地址不能为空！"}');
}
if (strpos($value, 'http') === false) {
	$longurl = 'http://' . daddslashes($value);
} else {
	$longurl = daddslashes($value);
}
if (preg_match('/^http[s]?:\/\//', $longurl)) {
	if (!strexists($value, '.')) {
		exit('{"code":-1,"msg":"请您输入正确的域名！"}');
	}
	$url_arr = parse_url($longurl);
	$domain = $url_arr['host'];
	$row = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . $domain . '\' limit 1');
	if (empty($row)) {
		$row = $DB->get_row('SELECT * FROM meihe_list WHERE domain=\'' . get_host($domain) . '\' and `fhy`=\'2\' limit 1');
	}
	if (empty($row)) {
		exit('{"msg":"当前短链接口会员才能使用！"}');
	}
	if ($row['type'] == 1) {
		if ($row['endtime'] <= $date) {
			if ($conf['hydq'] == 1) {
				$DB->query('DELETE FROM meihe_list WHERE domain=\'' . $domain . '\' ');
				exit('{"code":10,"msg":"当前域名会员已到期！请联系站长续费"}');
			} else {
				exit('{"code":10,"msg":"当前域名会员已到期！请联系站长续费"}');
			}
		}
	} else {
		$blacklist = explode('|', $conf['lhbamd']);
		if (in_array($domain, $blacklist)) {
			exit('{"code":-1,"msg":"当前域名已被拉黑，无法生成！"}');
		}
	}
	$count = $DB->count('SELECT count(*) FROM meihe_report WHERE url=\'' . $value . '\'');
	if ($row['type'] != 1) {
		if ($count > $conf['sccs']) {
			if ($conf['cgcs'] == 1) {
				$DB->query('insert into `meihe_list` (`domain`,`date`,`type`) values (\'' . $domain . '\',\'' . $date . '\',\'2\')');
			}
			exit('{"code":-1,"msg":"免费生成次数用完，已禁止生成！"}');
		}
	}
	$title = daddslashes($_REQUEST['title']);

	$sbm = daddslashes($_REQUEST['sbm'] ?? '');
	if ($sbm) {
		if ($row['type'] != 1) {
			exit('{"code":2,"msg":"自定义标题只能会员享受！"}');
		}
		if (strlen($sbm) < 6) {
			exit('{"code":2,"msg":"识别码不能低于6位！"}');
		}
		$uid2 = $sbm;
	} else {
		$uid2 = rand(0x0069f6bc7, 0x000003b9ac9ff);
	}
	if ($row['type'] != 1) {
	}

	$current_time = time();
	if ($conf['hyjmsc'] != 0 && isset($_SESSION['last_submit_time'])) {
		$time_difference = $current_time - $_SESSION['last_submit_time'];
		$remaining_seconds = $conf['hyjmsc'] - $time_difference;
		if ($time_difference < $conf['hyjmsc']) {
			exit('{"code":-1,"msg":"生成频率太高！请等待' . $remaining_seconds . '秒后再生成"}');
		}
	}

	$apihost = $DB->get_row("SELECT * FROM meihe_api WHERE sbm='{$vvalue}' limit 1");
	if (!$apihost || $apihost['jktz'] != 1) {
		exit('{"msg":"短链类型不存在！"}');
	}
	if (!$apihost || $apihost['jkfl'] != 1 && $row['type'] != 1) {
		exit('{"msg":"当前短链接口会员才能使用！"}');
	}
	if ($apihost['dljk'] == 3) {
		$dljk = $apihost['apiurl'];
	} elseif ($apihost['dljk'] == 4) {
		$dljk = $apihost['djyum'] . 'fhapi.php?format=txt&type=' . $apihost['sbm'] . '&longurl=';
	} elseif ($apihost['dljk'] == 5) {
		$dljk = $apihost['duanl'] . 'ajax.php?act=dwzapi&url=';
	}
	if (!$conf['urlapi']) {
		exit('{"msg":"没有生成接口，联系站长添加"}');
	}
	$uid = shorturl($value);
	$resulturl = ($header ?? '') . $conf['urlapi'] . $fhb . $uid2;

	if ($apihost['format'] == 1) {
		$tqq_url = \network\http\get($dljk . urlencode($resulturl), [], ['User-Agent' => 'BaiduSpider'])->body();
		if (!$tqq_url) {
			exit('{"code":-1,"msg":"生成失败请更换短链接口！"}');
		}
	} elseif ($apihost['format'] == 2) {
		$tqq_url = \network\http\get($dljk . urlencode($resulturl), [], ['User-Agent' => 'BaiduSpider'])->body();
		$tqq_url = json_decode($tqq_url, true);
		$tqq_url = $tqq_url['' . $apihost['fhzd'] . ''];
		if (!$tqq_url) {
			exit('{"code":-1,"msg":"生成失败请更换短链接口！"}');
		}
	}

	$tqq_url = trim($tqq_url);

	$tqq_url = 'https://www.zibll.com/?golink=' . base64_encode($tqq_url);

	$myrow = $DB->get_row('select * from meihe_long where url=\'' . $value . '\' limit 1');
	if (!$myrow) {
		$uid = shorturl($value);
		$DB->query('insert into `meihe_long` (`uid`,`url`) values (\'' . $uid . '\',\'' . $value . '\')');
	}
	$_SESSION['last_submit_time'] = $current_time;

	if ($DB->query('insert into `meihe_report` (`title`,`url`,`duanl`,`scfl`,`upzid`,`uid`,`time`,`date`,`ip`,`reason`) values (\'' . $title . '\',\'' . $longurl . '\',\'' . $apihost['sbm'] . '\',\'1\',\'' . $userrow['zid'] . '\',\'' . $uid2 . '\',\'' . TIMESTAMP . '\',\'' . $date . '\',\'' . real_ip() . '\',\'' . $fhfsb . '\')')) {
		$_SESSION['user_login_mail'] = TIMESTAMP;
		if ($format === 'txt') {
			exit($tqq_url);
		} else {
			$client = new Client();
			$client->setopt([
				// x-www-form-urlencoded 传参
				CURLOPT_POST => true,
				CURLOPT_POSTFIELDS => http_build_query(['url' => $tqq_url])
			]);
			$client->header([
				'Accept: application/json, text/javascript, */*; q=0.01',
				'Accept-Encoding: gzip, deflate, br, zstd',
				'Accept-Language: zh-CN,zh;q=0.9',
				'Connection: keep-alive',
				'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
				'Host: t.doruo.cn',
				'Origin: https://t.doruo.cn',
				'Referer: https://t.doruo.cn/',
				'Sec-Fetch-Dest: empty',
				'Sec-Fetch-Mode: cors',
				'Sec-Fetch-Site: same-origin',
				'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
				'X-Requested-With: XMLHttpRequest',
				'sec-ch-ua: "Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
				'sec-ch-ua-mobile: ?0',
				'sec-ch-ua-platform: "Windows"'
			]);
			$doruo = $client->send('https://t.doruo.cn/convert')->toArray('data');
			if (!empty($doruo)) {
				$tqq_url = 'https://t.doruo.cn/' . explode('*', $doruo)[0];
			}

			$result = array('code' => 1, 'ae_url' => $tqq_url, 'haohao_api' => $tqq_url, 'haohao_url' => $longurl);

			exit(json_encode($result));
		}
	} else {
		exit('{"msg":"生成失败请更换短链接口！"}');
	}
} else {
	exit('{"msg":"请输入正确的网址或域名"}');
}
